home *** CD-ROM | disk | FTP | other *** search
- /* This file contains several examples that I have written. They may be
- * Used freely for personal non-profit purposes. All have been tested
- * on a VERSION 7 compatible machine, and appear to work O.K.
- * Some of the examples can be turned into functional programs easily.
- * Gerald Shrum. (316) 584-2501 30 Aug., 1985.
- */
-
- /* The 'fork' system call starts a new proccess.
- This is an example program which starts a child and monitors the progress
- of both the child and the parent
- To see what happens when parent completes before child, make child_loop
- larger then parent_loop */
- /* Gerry Shrum 22 Feb, 1985 */
-
-
- /*---->> Includes and LIB definitions: preprocessor <<----<< */
- #include <stdio.h>
- #include <signal.h>
- #include <calls.h>
-
- /*---->> Globals and declarations <<----<< */
- int pid; /* Child process ID */
- int status; /* Child status */
-
- /*---->> Functions and modules: <<----<< */
- /* None except the predefined system functions:
- exit();
- fork();
- kill();
- printf();
- onalarm();
- sleep();
- See system manual pages for these functions and system calls.
- */
-
- /*---->> Program main: <<----<< */
- main()
- {
- int i, p, sec = 5;
- int child_loop = 5; /* Number of times the child loops <<-- User setable */
- int parent_loop = 10;/* Number of times the parent loops <<- User setable */
-
- printf("Start of fork example\n");
- pid = fork(); /* call fork to start the child process */
- /* A confusing point is that pid is returned to both
- parent and child. The child's real pid is returned
- to the parent, while 0 is returned to the child iff
- it was started, else the child receives -1 */
-
- if (pid == 0) /* iff child was started */
- {
- printf("Child started\n");
- status = 1;/* Let parent know */
- for (i=0; i< child_loop; ++i)/* Now, loop until we reach the
- number of loops in 'child_loop' */
- { /* Child loop body */
- printf("Child still here\n");
- sleep(sec);
- }
-
- printf("Child completed!\n");/* child completed before parent */
- status = 0;
- exit(0);/* Child goes zomb until parent completes, */
- } /* then system removes them both from the pid list */
-
- /* Parent comes here */
- printf("Parent arrived\n");
-
- if(pid <= 0)
- {
- printf("Couldn't start child.. Aborting!\n");
- exit(0);
- }
-
- for (p = 0; p < parent_loop; ++p) /* loop and monitor status */
- {
- printf("Parent: Child PID = %d \n",pid);
- sleep(sec);
- }
-
- if(status == 1) /* kill the child if it is still running */
- {
- printf("Parent completed, killing child %d",pid);
- kill(pid,SIGKILL);
- }
-
- printf("Parent completed.\n");
- exit(0);
- }
-
- /* Program timeout; Source timeout.c */
- /* timeout: set time limit on a process */
- /* This program includes an example of the 'fork' system call.
- It also includes the pre-defined system function 'onalarm()'.
-
-
- #include <stdio.h>
- #include <signal.h>
- #include <calls.h>
- int pid; /* child process id */
- char *progname;
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int sec = 10, status, onalarm();
-
- progname = argv[0];
- if (argc > 1 && argv[1][0] == '-') {/* Then we have 'time' */
- sec = atoi(&argv[1][1]);
- argc--;
- argv++;
- }
- if (argc < 2)/* Then we do not have a program to time */
- {
- printf("\n :Usage: timeout [-<t>] [command]\n");
- printf("Where <t> = timeout count in seconds from 1 to 99\n")
- printf("And [command] is the command to be ran.\n");
- printf("example: timeout -10 cat afile\n");
- exit(1);
- }
- if ((pid=fork()) == 0) {
- execv(argv[1], &argv[1]);
- /* only returns on error */
- fprintf(stderr,"\nCouldn't start %s.\n",argv[1]);
- exit(errno);
- }
- signal(SIGALRM,onalarm);
- alarm(sec);
- if (wait(&status) == -1 || (status & 0177) !=0)
- fprintf(stderr,"Timeout: %s killed\n", argv[1]);
- exit(errno);
- }
-
- onalarm() /* kill child when alarm arrives */
- {
- kill(pid, SIGKILL);
- }
-
- /* End of timeout.c */
-
-
- /* example of index. Gerry Shrum Nov, 1984 */
- /* index returns a memory pointer. this routine converts it to string pos. */
-
- char tstr[] = {"abcdefghijklmnopqrstuvwxyz1234567890-=,./;':<>?~"};
-
- main()
- {
- char cch, inp[81];
- int res,fnd, ptr;
-
- fnd = &tstr;
- while(cch != 'Q')
- {
- printf("Enter character to be found (Q to quit).");
- scanf ("%c",inp);
- cch = inp[0];
- ptr = index(tstr,cch);
- res = ptr - fnd;
- if(ptr == 0) {printf("Not found\n");}
- else printf("%c = position %d\n",cch,res);
- }
- }/* End of index.c */
-
-
-
- /* Pipe1; pipe example. This file contains the parent. The child
- is contained in pipe2_exam.c. Compile seperately and link together.
- E.G.: icc pipe1_exam; icc pipe2_exam
- ilink pipe1_exam pipe2_exam
- Gerry Shrum, Nov., 1984.
- */
- /*---->> Includes and LIB definitions: preprocessor <<---- << */
- #include <stdio.h>
- #include <sgtty.h>
- #include <signal.h>
-
- int pid; /* Child process ID */
- int status; /* Child status */
- int fildes[2]; /* The pipe file descriptor */
- char out_buf[MAXLINE]; /* buffer for output */
- char in_buf[MAXLINE]; /* buffer for input */
- char *out_ptr; /* Pointer to char to be output */
-
- /* ***** ******* ******* ******* ******* ******* ******* ******* ******* **** */
- main()
-
- {
- int i, in_stat, child_proc, yy;
-
- in_stat = pipe(fildes);/* 0 returned iff pipe was opened, else -1 */
- if(in_stat == -1)/* too many files open */
- {
- printf("Can't open pipe!\n");
- exit(in_stat);
- }
- printf("pipe_rd =%d, pipe_wrt =%d, rtn =%d\n",fildes[0],fildes[1],in_stat);
- pid = fork();
- if (pid == 0) /* Iff child was started */
- {
- write_pipe();
- printf("Pipe broke; Aborting...\n");
- status = 0;
- exit(0); /* Iff child ever returns */
- }
- /* Parent comes here */
- if(pid <= 0)
- {
- printf("Couldn't start child; Aborting...");
- exit(14);
- }
- printf("Child started as %d, status =%d\n",pid,status);
- close(fildes[1]); /* this function doesn't write */
- for(i=0;i<10;++i)
- {
- sleep(1);
- yy = read(fildes[0],out_buf,20);/* Read from child */
- printf("Read %d characters.%s\n",yy,out_buf);
- if (yy == -1 ) {printf("Read pipe status =%d\n",yy);}
- }
-
- close(fildes[0]); /* close the read pipe */
- kill(pid,SIGKILL); /* Terminate the child */
- exit(0) ;
- } /* End of pipe1.c */
-
-
-
-
- /* Pipe example #2, child process writing to parent. */
- /* Gerry Shrum, Jan 1985 */
- /* write from this child process to the calling process */
- /* fildes[0] is the read descriptor from the parent pipe call */
- /* fildes[1] is the write descriptor from the parent pipe call */
-
- /*---->> Includes and LIB definitions: preprocessor <<----<< */
- #include <stdio.h>
-
- /*---->> Globals and declarations <<----<< */
- extern int fildes[2];
-
- /*---->> Functions and modules: <<----<< */
- VOID write_pipe()
- {
- char bufin[24];
- int i, fd, rstat;
-
- *bufin = "Writing into a pipe'\n'";
- fd = fildes[1];
- close(fildes[0]);/* This function doesn't read */
- for(i=0;i<10;++i)
- {
- /* write on the pipe */
- rstat = write(fd,bufin,20);
- if(rstat == -1) {printf("pipe write error %d\n",rstat);}
- }
- close(fildes[1]);
- exit(0) ;
- } /* End of pipe2.c */
-
- /* End of file */
- /* End of exams.c */
- ro